package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.BannerDao;
import com.sixth.entity.Banner;
import com.sixth.entity.Head;
import com.sixth.entity.TableResult;
import com.sixth.service.BannerService;
import com.sixth.until.CommonResult;
import com.sixth.until.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: self-tui-education
 * @description:
 * @author: C
 * @create: 2022-04-21 15:57
 **/
@Service
public class BannerServicelmpl implements BannerService {
@Autowired
private BannerDao bannerDao;
    @Override
    public TableResult selectAllHead(Integer pageNum, Integer pageSize, Banner banner) {
        PageHelper.startPage(pageNum,pageSize);
        String title = banner.getTitle();
        String isDisable = banner.getIsDisable();
        System.out.println(title+isDisable);
        List<Banner>banners= bannerDao.selectbannerByCondition(title,isDisable);
        PageInfo<Banner> pageInfo = new PageInfo<Banner>(banners);

        return new TableResult(2000,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }



    @Override
    public CommonResult deleteFindById(Banner banner) {
        String headId=banner.getId();
        int row =bannerDao.deleteByPrimaryKey(headId);
        if (row>0){
            return new CommonResult(2000,"删除成功","null");
        }else {
            return new CommonResult(5000,"删除失败","null");
        }
    }

    @Override
    public CommonResult changeStatus(String id, String isDisable) {
        int i = bannerDao.changeStatusByid(id,isDisable);
        if (i > 0) {
            return new CommonResult(2000,"修改状态成功",null);
        }
        return new CommonResult(2000,"修改失败",null);
    }

    @Override
    public CommonResult addZone(Banner banner) {
        String id= OrderNoUtil.getOrderNo();
        Date gmtCreate = banner.getGmtCreate();
        Date gmtModified = banner.getGmtModified();
//        String startTime = null;
//        String editTime = null;
//        if (gmtCreate != null) {
//            try {
//                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//                startTime = null;
//                editTime = null;
//                String create = gmtCreate + "";
//                String modified = gmtModified + "";
//                Date format1 = new Date(create);
//                Date format2 = new Date(modified);
//                String start = format.format(format1);
//                String edit = format.format(format2);
//                startTime = format.parse(start).toLocaleString();
//                editTime = format.parse(edit).toLocaleString();
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//        }
        int i = bannerDao.addZone(id,banner.getTitle(),banner.getImageUrl(),banner.getLinkUrl(),banner.getSort(),"0",gmtCreate,gmtModified,"0");
        if (i !=0){
            return new CommonResult(2000,"添加成功",null);
        }
        return new CommonResult(2000,"添加失败",null);
    }


    @Override
    public CommonResult UpdateZone(Banner banner) {
        int i = bannerDao.updateByPrimaryKeySelective(banner);
        if (i != 0) {
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

}
